package cc.rengu.igas.route.core.model;

import cc.rengu.igas.route.common.enums.RouteTxnNumEnum;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.DstTxnAuthCfg;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 指定路由的全量信息的基础信息不包括各类型的路由配置信息
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/08/05
 */
public class CompleteRouteInfoBase {

    /**
     * 当前智能路由支持的所有可用通道及相关信息
     */
    private Map<String, CompleteRouteInfoBean> availDstChannels;

    public CompleteRouteInfoBase() {
        availDstChannels = new LinkedHashMap<>();
    }

    public Map<String, CompleteRouteInfoBean> getAvailDstChannels() {
        return availDstChannels;
    }

    public boolean addAvailDstChannel(String txnGroup, CompleteRouteInfoBean completeRouteInfoBean) {
        if (null == completeRouteInfoBean.getDstChannelInfo() ||
                null == completeRouteInfoBean.getDstTxnAuthCfg() ||
                null == completeRouteInfoBean.getChannelCallCfg() ||
                StringUtil.isEmptyOrNull(completeRouteInfoBean.getDstChannelInfo().getDstChannelId()) ||
                StringUtil.isEmptyOrNull(completeRouteInfoBean.getChannelCallCfg().getTxnNum())) {
            return false;
        }
        RouteTxnNumEnum txnNumEnum = RouteTxnNumEnum.getInstanceFromTxnGroup(txnGroup);
        if (null == txnNumEnum) {
            return false;
        }
        if (!txnNumEnum.getTxnNum().equals(completeRouteInfoBean.getChannelCallCfg().getTxnNum())) {
            return false;
        }
        String key = completeRouteInfoBean.getDstChannelInfo().getInstId().trim() + ":" + completeRouteInfoBean.getDstChannelInfo().getDstChannelId() + ":" + txnNumEnum.getTxnNum();
        availDstChannels.put(key, completeRouteInfoBean);
        return true;
    }

    public boolean removeAvailDstChannel(String txnGroup, CompleteRouteInfoBean completeRouteInfoBean) {
        if (null == completeRouteInfoBean.getDstChannelInfo() ||
                null == completeRouteInfoBean.getDstTxnAuthCfg() ||
                null == completeRouteInfoBean.getChannelCallCfg() ||
                StringUtil.isEmptyOrNull(completeRouteInfoBean.getDstChannelInfo().getDstChannelId()) ||
                StringUtil.isEmptyOrNull(completeRouteInfoBean.getChannelCallCfg().getTxnNum())) {
            return false;
        }
        RouteTxnNumEnum txnNumEnum = RouteTxnNumEnum.getInstanceFromTxnGroup(txnGroup);
        if (null == txnNumEnum) {
            return false;
        }
        if (!txnNumEnum.getTxnNum().equals(completeRouteInfoBean.getChannelCallCfg().getTxnNum())) {
            return false;
        }
        String key = completeRouteInfoBean.getDstChannelInfo().getInstId().trim() + ":" + completeRouteInfoBean.getDstChannelInfo().getDstChannelId() + ":" + txnNumEnum.getTxnNum();
        availDstChannels.remove(key);
        return true;
    }

    public CompleteRouteInfoBean getAvailDstChannel(String instId, String dstChannelId, String txnGroup) {
        RouteTxnNumEnum txnNumEnum = RouteTxnNumEnum.getInstanceFromTxnGroup(txnGroup);
        if (null == txnNumEnum) {
            return null;
        }
        return availDstChannels.get(instId.trim() + ":" + dstChannelId + ":" + txnNumEnum.getTxnNum());
    }

    public DstChannelInfo getDstChannelInfo(String instId, String dstChannelId, String txnGroup) {
        CompleteRouteInfoBean completeRouteInfoBean = getAvailDstChannel(instId, dstChannelId, txnGroup);
        if (null == completeRouteInfoBean) {
            return null;
        }
        return completeRouteInfoBean.getDstChannelInfo();
    }

    public ChannelCallCfg getChannelCallCfg(String instId, String dstChannelId, String txnGroup) {
        CompleteRouteInfoBean completeRouteInfoBean = getAvailDstChannel(instId, dstChannelId, txnGroup);
        if (null == completeRouteInfoBean) {
            return null;
        }
        return completeRouteInfoBean.getChannelCallCfg();
    }

    public DstTxnAuthCfg getDstTxnAuthCfg(String instId, String dstChannelId, String txnGroup) {
        CompleteRouteInfoBean completeRouteInfoBean = getAvailDstChannel(instId, dstChannelId, txnGroup);
        if (null == completeRouteInfoBean) {
            return null;
        }
        return completeRouteInfoBean.getDstTxnAuthCfg();
    }
}
